﻿using System;
using ZFine.Application.Gas;
using ZFine.Application.SystemManage;
using ZFine.Code;
using ZFine.Domain.Entity.Gas;
using ZFine.Domain.Entity.SystemManage;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;

namespace ZFine.Web.Areas.Gas.Controllers
{
    public class CompanyController : ControllerBase
    {
        private CompanyApp companyApp = new CompanyApp();
        private ItemsDetailApp itemsDetailApp = new ItemsDetailApp();
        [HttpGet]
        [HandlerAuthorize(ignore:false)]
        public override ActionResult Index()
        {
            return View();
        }
        [HttpGet]
        [HandlerAuthorize(ignore: false)]
        public override ActionResult Form()
        {
            return View();
        }
        [HandlerAuthorize(ignore: false)]
        public override ActionResult Details()
        {
            return View();
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetGridJson(string keyword)
        {
            List<CompanyEntity> companyEntities =companyApp.GetList(keyword);
            List<ItemsDetailEntity> itemsDetailEntities = itemsDetailApp.GetItemList("Area");

            var data = from company in companyEntities
                       join area in itemsDetailEntities
                       on company.AreaId equals area.F_ItemCode
                       select new
                       {
                           company.F_Id,
                           company.CompanyName,
                           company.AreaId,
                           AreaName=area.F_ItemName,
                           company.Media,
                           company.DispenserMaker,
                           company.DispenserCount,
                           company.DispenserGunCount,
                           company.Remark
                       };

            return Content(data.ToJson());
            
        }
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetFormJson(string keyValue)
        {
            var data = companyApp.GetForm(keyValue);
            return Content(data.ToJson());
        }
        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult SubmitForm(CompanyEntity companyEntity, string permissionIds, string keyValue)
        {

            companyApp.SubmitForm(companyEntity, keyValue);
            if (string.IsNullOrEmpty(keyValue))
            {
                return Success("新增成功，默认密码为123456");
            }
            else
            {
                return Success("操作成功。");
            }
              
        }
        [HttpPost]
        [HandlerAjaxOnly]
        [HandlerAuthorize(ignore:false)]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteForm(string keyValue)
        {
            companyApp.DeleteForm(keyValue);
            return Success("删除成功。");
        }
        [HttpGet]
        public ActionResult GetTest(string s)
        {
            CompanyEntity c= new CompanyEntity()
            {
                Address = s
            };
            return Content(c.ToJson());
        }
    }
}
